﻿/*********************物流项目三组************************
*  功能描述：车辆管理
*  作者：黄启轩
*  时间：2022-01-10
*  修改人：黄启轩
*  修改时间：2022-01-10
*********************物流项目三组************************/

using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using TodoApp.TMS_personnelmatters.Employeess;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_Basics.TMS_Vehicles
{
    public class TMS_VehicleModel : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_VehicleModel
    {
        public TMS_VehicleModel(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 显示车辆信息

        public async Task<PageResponse<TMS_VehicleModelDto>> GetVehicles(string V_model,string V_licenseplate,string V_drivere,string V_company, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_VehicleModelDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_VehicleModel where 1=1 ";
            if (!string.IsNullOrEmpty(V_model))
            {
                sql += $" and V_model ='{V_model}'";
            }
            if (!string.IsNullOrEmpty(V_licenseplate))
            {
                sql += $" and V_licenseplate ='{V_licenseplate}'";
            }
            if (!string.IsNullOrEmpty(V_drivere))
            {
                sql += $" and V_drivere ='{V_drivere}'";
            }
            if (!string.IsNullOrEmpty(V_company))
            {
                sql += $" and V_company ='{V_company}'";
            }

            var list = await dbConnection.QueryAsync<TMS_VehicleModelDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_VehicleModelDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }


        #endregion

        #region 车辆信息添加

        public async Task<int> CreateVehic(AddDtoVehicles info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_VehicleModel values ('{info.Id}','{info.V_model}','{info.V_licenseplate}','{info.V_drivere}','{info.V_company}','{info.V_lon}','{info.V_width}','{info.V_hight}','{info.V_color}','{info.V_titem}','{info.V_ynumber}','{info.V_stime}','{info.V_km}','{info.V_etime}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 车辆信息删除
        public async Task<int> DeleteVehicle(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_VehicleModel where Id='{Id}'",null,
                 await GetDbTransactionAsync());

        }

        #endregion
    }
}
